<?php

namespace App\Console\Commands;

use App\Models\Teachers;
use Illuminate\Console\Command;

class UpdateBaseInfoTeacher extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ClearRedundantData:teacher';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '清除教师信息数据表中冗余数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("---清除,开始:".date('Y-m-d H:i:s')."---");
        $this->taskRun();
        // Log::channel('daily')->info('数据');
        $this->info("---清除,结束:".date('Y-m-d H:i:s')."---");
    }

    public function taskRun()
    {
        $teacher = Teachers::where('deleted', 0)->get();

        $teacher->groupBy('user_id')->each(function($item, $key) {
            $maxItem = $item->where('data_version_id', $item->max('data_version_id'))->first();
            Teachers::where('user_id', $key)->where('id', '!=', $maxItem->id)->update(['deleted' => 1]);
        });
    }
}
